<?php
abstract class BaseController
{
    protected $http_method;
    protected $input;
    protected $m;
    protected $output;
    protected $uri;
    protected function __construct($uri)
    {
        $this->uri  = $uri;
        $this->output = ConfLib::get('http', 'output');
        $this->http_method = strtolower($_SERVER['REQUEST_METHOD']);
        switch($this->http_method) {//todo cookie
            case 'get' :
                $this->input = $_GET;
                break;
            case 'post' :
                $this->input = $_POST;
                break;
            case 'put' :
                $this->input = file_get_contents('php://input');
                break;
        }
    }

    protected function checkLogin()
    {
        if(isset($_COOKIE['access_token'])) {
            $m = new UserModel();
            //todo catch
            $token = $m->decryptToken($_COOKIE['access_token']); //todo move to __construct
            //$user = $m->get($token['user_id']);
            return $token;
        }
        $this->output['http']['status_code'] = 302;
        $www = ConfLib::get('uri', 'www_domain');
        $this->output['http']['uri'] = 'http://' . $www . '/accounts/login';
        throw new HttpException('', 302, $this->output);
    }
 
    protected function checkParams($data)
    {
        foreach($data as $one) {
            if(!isset($this->input[$one])) {
                throw new HttpException('', 400);
            }
        }
    }
}
?>
